const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HTMLWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  entry: "./src/index.js",
  output: {
    // 产物输出
    filename: "main.js",
    path: path.resolve(__dirname, "dist"),
  },
  module: {
    rules: [
      // 配置css解析器
      {
        test: /\.css$/i,
        // use: ["style-loader", "css-loader"],
        use: [
          // 根据运行环境判断使用那个 loader
          process.env.NODE_ENV === "development"
            ? "style-loader"
            : MiniCssExtractPlugin.loader,
          // "css-loader",
          //! 添加postcss 解析器
          // "postcss-loader"
          {
            loader: "css-loader",
            options: {
              importLoaders: 1,
            },
          },
          "postcss-loader",
          // ! 使用配置
          // {
          //   loader: "postcss-loader",
          //   options: {
          //     postcssOptions: {
          //       // 添加 autoprefixer 插件
          //       plugins: [require("autoprefixer")],
          //     },
          //   },
          // },
        ],
      },
      // 配置less
      {
        test: /\.less$/,
        // 不互斥 less和post-css
        use: ["style-loader", "css-loader", "postcss-loader", "less-loader"],
      },
    ],
  },
  plugins: [new MiniCssExtractPlugin(), new HTMLWebpackPlugin()],
};
